<template>
  <div style="position: relative">
    <el-dialog center :visible.sync="deflag" title="确认删除" width="30%">
      <div slot="footer" class="dialog-footer" style="width: 100%;display: flex;justify-content: space-between">
        <el-button @click="deflag = false">取 消</el-button>
        <el-button type="primary" @click="DelThis">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="dialogTitle"
      center
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <div style="width: 100%;overflow: auto;height: 50vh">
        <el-form ref="userForm" :model="formData" label-width="80px">
          <el-form-item label="用户名" prop="username" v-if="dialogTitle==='新增用户'">
            <el-input v-model="formData.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" v-if="dialogTitle==='新增用户'">
            <el-input v-model="formData.password"></el-input>
          </el-form-item>
        <el-form-item label="头像" prop="url">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:8080/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="formData.url" :src="formData.url" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="formData.sex">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formData.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="type">
<!--          指导教师，评阅教师，答辩教师-->
          <el-select v-model="formData.type" placeholder="请选择角色">
            <el-option label="管理员" value="管理员"></el-option>
            <el-option label="老师" value="老师"></el-option>
            <el-option label="普通用户" value="普通用户"></el-option>
          </el-select>
        </el-form-item>
        <!-- 其他表单项 -->
      </el-form>
      <div slot="footer" class="dialog-footer" style="width: 100%;display: flex;justify-content: space-between">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveUser">确 定</el-button>
      </div>
      </div>
    </el-dialog>
    <el-button type="primary" style="margin-left: 90%; margin-top: 2vh; margin-bottom: 2vh" @click="openDialog('add',null)">新增用户</el-button>
    <el-table width="100%" height="70vh" :data="List">
        <el-table-column label="序号"  align="center">
          <template slot-scope="scope">
            {{(currentPage-1)*pageSize+scope.$index+1}}
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="name" align="center"></el-table-column>
        <el-table-column label="性别" prop="sex" align="center"></el-table-column>
        <el-table-column label="邮箱" prop="email" align="center"></el-table-column>
        <el-table-column label="电话" prop="phone" align="center"></el-table-column>
        <el-table-column label="头像" align="center">
          <template slot-scope="scope">
            <el-avatar :src="scope.row.url" size="30"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column label="角色" prop="type" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="info" size="mini" @click="openDialog('edit',scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="ShowDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
    </el-table>
    <center>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </center>
  </div>
</template>

<script>
import { GetList, Remove, SaveUser } from '@/api/user'

export default {
  name: 'User',
  data() {
    return {
      dialogVisible: false,
      dialogTitle: '',
      List: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      formData: {
        id:null,
        url: '',
        username:"",
        password:"",
        name: '',
        sex: 1,
        email: '',
        phone: '',
        type: '',
        percent: ''
      },
      id:"",
      deflag:false
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    DelThis(){
      let that=this
      Remove(this.id).then(()=>{
        that.currentPage=1
        that.getList()
        that.deflag=false
      })
    },
    getList() {
      const params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize
      };
      GetList(params).then(res => {
        this.List = res.userPage.records;
        this.total = res.userPage.total;
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
      this.$message.success("已修改")
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    openDialog(mode, data) {
      this.dialogTitle = mode === 'add' ? '新增用户' : '编辑用户';
      this.dialogVisible = true;
      if (mode === 'edit' && data) {
        this.formData = { ...data };
      } else {
        // Reset form data for add mode
        this.$refs.userForm.resetFields();
        this.formData = {
          id:null,
          url: '',
          username:"",
          password:"",
          name: '',
          sex: 1,
          email: '',
          phone: '',
          type: ''
        };
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    handleAvatarSuccess(response, file) {
      this.formData.url =response.url
      this.$message.success("上传成功")
      // Here you can handle the response from your upload API and update formData.url with the actual URL
      // For now, we're just using the object URL of the uploaded image
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isPNG) {
        this.$message.error('上传头像图片只能是 JPG 或 PNG 格式！');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB！');
      }
      return (isJPG || isPNG) && isLt2M;
    },
    ShowDel(id){
      this.id=id
      this.deflag=true
    },
    saveUser() {
      SaveUser(this.formData).then(res=>{
        this.currentPage=1
        this.getList()
        this.$message.success("已保存")
      })
      // After saving, you might want to close the dialog
      this.dialogVisible = false;
    }
  }
}
</script>

<style scoped lang="scss">
.avatar-uploader {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 150px;
  height: 150px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  overflow: hidden;
}

.avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
}
</style>
